﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Configuration;

using MSAcademic.v2.Infrastructure.Login;
using MSAcademic.v2.Infrastructure.User;
using MSAcademic.v2.Infrastructure.Utility;

namespace MSAcademic.v2.Web.Areas.Admin.Models.Login
{
    public partial class RegistrationModel : IDataErrorInfo
    {
        public bool IsFormSuccess { get; set; }
        public ActingRole ActingRole { get; set; }

        public string RegToken { get; set; }

        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, MinimumLength = 4, ErrorMessage = "Korisničko ime mora biti između 4 i 40 znakova duljine!")]
        public string Username { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, MinimumLength = 6, ErrorMessage = "Lozinka mora biti između 6 i 40 znakova duljine!")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, MinimumLength = 6, ErrorMessage = "Lozinka mora biti između 6 i 40 znakova duljine!")]
        public string RepeatedPassword { get; set; }

        [Required(ErrorMessage = "Polje ne smije biti prazno!")]
        [StringLength(40, ErrorMessage = "Email može biti maksimalno 50 znakova duljine!")]
        public string Email { get; set; }
    }

    public partial class RegistrationModel
    {
        public IUser User
        {
            get
            {
                if (_user == null)
                {
                    _user = new Lazy<IUser>(() => _loginService.TryGetSignedInUser());
                }
                return _user.Value;
            }
        }
        private Lazy<IUser> _user;

        public bool IsTokenValid
        {
            get
            {
                if (_isTokenValid == null)
                {
                    _isTokenValid = new Lazy<bool>(() => this.RegToken == ConfigurationManager.AppSettings["TokenMsp"] || this.RegToken == ConfigurationManager.AppSettings["TokenPripravnik"] || this.RegToken == ConfigurationManager.AppSettings["TokenVanjskiSuradnik"]);
                }
                return _isTokenValid.Value;
            }
        }
        private Lazy<bool> _isTokenValid;
    }

    public partial class RegistrationModel
    {
        public string this[string prop]
        {
            get
            {
                if (prop == "Username")
                {
                    if (this.Username != null)
                    {
                        if (_userQueries.TryGetWhereUsername(this.Username) != null)
                        {
                            return "Korisnik s tim korisničkim imenom već postoji!";
                        }
                    }
                }

                if (prop == "Password")
                {
                    if (this.Password != this.RepeatedPassword)
                    {
                        return "Unesene lozinke nisu iste!";
                    }
                }

                if (prop == "Email")
                {
                    if (!_textService.IsValidEmail(this.Email))
                    {
                        return "Oblik e-maila nije ispravan (npr. info@msacademic.hr).";
                    }
                }

                return null;
            }
        }

        public string Error
        {
            get { return null; }
        }
    }

    public partial class RegistrationModel
    {
        private readonly ILoginService _loginService;
        private readonly IUserQueries _userQueries;
        private readonly Text _textService;

        public RegistrationModel(
            ILoginService loginService,
            IUserQueries userQueries,
            
            Text textService)
        {
            _loginService = loginService;
            _userQueries = userQueries;
            _textService = textService;
        }
    }
}